/**
 * @file STL_HttpParser.h
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 
 * @version 0.1
 * @date 2021-02-18
 * 
 * @copyright Copyright Sci-Tech Laboratories (c) 2021
 * 
 */

#ifndef __STL_HTTP_PARSER_H__
#define __STL_HTTP_PARSER_H__

#include <string>
#include <memory>
#include <map>
#include <vector>
#include <regex>
#include <functional>
#include "STL_Tools.h"
#include "STL_HttpTools.h"
#include "STL_HttpRequest.h"

namespace STL
{
    class STL_HttpParser
    {
    private:
        typedef enum Status_e
        {
            Wait_Request_Line,
            Wait_Request_Header,
            Wait_Request_Body
        } Status;

    public:
        typedef std::function<void(int /*socketId*/, std::shared_ptr<STL_HttpRequest> /*req*/)> RequestCallback;

    public:
        STL_HttpParser(int socketId)
        {
            m_body_lenght = 0;
            m_status = Wait_Request_Line;
            m_data.clear();
            m_socketId = socketId;
        };
        ~STL_HttpParser() = default;

    public:
        STL_SYNTHESIZE(STL_HttpParser::RequestCallback, m_RequestCallback, RequestCallback);

    public:
        void addData(std::string &data)
        {
            m_data.append(data);
            parse();
        };
        void addData(std::shared_ptr<std::string> sp_data)
        {
            m_data.append(sp_data->data(), sp_data->size());
            parse();
        };

    private:
        void parse();
        void parseUri();
        void parseQuery();

    private:
        std::shared_ptr<STL_HttpRequest> m_request;
        size_t m_body_lenght;
        STL_HttpParser::Status m_status;
        int m_socketId;
        std::string m_data;
    };
}

#endif //__STL_HTTP_PARSER_H__
